{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2bec7c1b-3e7b-41e8-b8cb-07d14fb897ed",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax. Perhaps you forgot a comma? (3269555335.py, line 6)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[1], line 6\u001b[1;36m\u001b[0m\n\u001b[1;33m    transforms.RandomDistort(brightness_range=0.9,brightness_prob=0.5,contrast_\u001b[0m\n\u001b[1;37m                                                                      ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax. Perhaps you forgot a comma?\n"
     ]
    }
   ],
   "source": [
    "import paddlex as pdx\n",
    "from paddle.cls import transforms\n",
    "\n",
    "train_transforms=transforms.Compose([transforms.RandomCrop(crop_size=224),\n",
    "                                    transforms.RandomHorizontalFlip(),\n",
    "                                    transforms.RandomDistort(brightness_range=0.9,brightness_prob=0.5,contrast_\n",
    "                                                            range=0.9,contrast_prob=0.5,saturation_range=0.9,saturation_prob=0.5,hue_range=18,\n",
    "                                                            hue_prob=0.5),\n",
    "                                    transforms.Normalize()\n",
    "                                    ])\n",
    "\n",
    "val_transforms=transforms.Compose([transforms.ResizeByShort(short_size=256),\n",
    "                                  transforms.CenterCrop(corp_size=224),\n",
    "                                  transforms.Normalize()\n",
    "                                  ])\n",
    "\n",
    "train_dataset=pdx.datasets.ImageNet(\n",
    "    data_dir='./data/garbage',\n",
    "    file_list='./train.txt',\n",
    "    label_list='./labels.txt',\n",
    "    transforms=train_transforms,\n",
    "    shuffle=True\n",
    ")\n",
    "\n",
    "val_dataset=pdx.datasets.ImageNet(\n",
    "    data_dir='./data/garbage',\n",
    "    file_list='./val.txt',\n",
    "    label_list='./labels.txt',\n",
    "    transforms=val_transforms\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "76861f56-9423-49b6-a176-6886e768c60b",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'paddlex'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[3], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mpaddlex\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mpdx\u001b[39;00m\n\u001b[0;32m      2\u001b[0m num_classes\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mlen\u001b[39m(train_dataset\u001b[38;5;241m.\u001b[39mlabels)\n\u001b[0;32m      3\u001b[0m model\u001b[38;5;241m=\u001b[39mpdx\u001b[38;5;241m.\u001b[39mcls\u001b[38;5;241m.\u001b[39mResNet50_vd_ssid(num_classes\u001b[38;5;241m=\u001b[39mnum_classes)\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'paddlex'"
     ]
    }
   ],
   "source": [
    "import paddlex as pdx\n",
    "num_classes=len(train_dataset.labels)\n",
    "model=pdx.cls.ResNet50_vd_ssid(num_classes=num_classes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b75e326-b312-4a7c-bfde-db1c65955dd2",
   "metadata": {},
   "outputs": [],
   "source": [
    "#启动模型进行训练\n",
    "model.train(num_epochs=5,#训练迭代轮数\n",
    "           train_dataset=train_dataset,#训练数据读取器\n",
    "           train_batch_size=16,#训练数据批次大小\n",
    "           eval_dataset=val_dataset,#验证数据读取器\n",
    "           lr_decay_epochs=[80,100,150],#优化器的学习衰减率轮数\n",
    "           save_interval_epochs=1,#保存模型的间隔\n",
    "           learning_rate=0.0002,#优化器的初始学习率\n",
    "           save_dir='output/ResNet50_vd_ssid',#保存模型的路径\n",
    "           use_vdf=True)#使用VisualDL查看训练指标的变化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "77c543f9-7158-4558-928d-7eca5384ffac",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'pdx' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[6], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m#导入最优模型\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m model\u001b[38;5;241m=\u001b[39mpdx\u001b[38;5;241m.\u001b[39mload_model(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124moutput/ResNet50_vd_ssid/best_model\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m      3\u001b[0m \u001b[38;5;66;03m#待预测图像的路径\u001b[39;00m\n\u001b[0;32m      4\u001b[0m image_name\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m./data/garbage/paper/paper10.jpg\u001b[39m\u001b[38;5;124m'\u001b[39m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'pdx' is not defined"
     ]
    }
   ],
   "source": [
    "#导入最优模型\n",
    "model=pdx.load_model('output/ResNet50_vd_ssid/best_model')\n",
    "#待预测图像的路径\n",
    "image_name='./data/garbage/paper/paper10.jpg'\n",
    "#进行预测\n",
    "result=model.predict(image_name)\n",
    "#输出预测结果\n",
    "print('Predict Result:',result)\n",
    "#提取分类结果\n",
    "number=result[0]['category']\n",
    "number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "09637ff9-781f-4097-b3a4-aba1ca823f35",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'paddlex'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[8], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m#导入paddlex\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mpaddlex\u001b[39;00m\n\u001b[0;32m      4\u001b[0m img_file\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m./data/garbage/paper/paper10.jpg\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m      5\u001b[0m model\u001b[38;5;241m=\u001b[39mpdx\u001b[38;5;241m.\u001b[39mload_model(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124moutput/ResNet50_vd_ssid/best_model\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'paddlex'"
     ]
    }
   ],
   "source": [
    "#导入paddlex\n",
    "import paddlex\n",
    "\n",
    "img_file='./data/garbage/paper/paper10.jpg'\n",
    "model=pdx.load_model('output/ResNet50_vd_ssid/best_model')\n",
    "\n",
    "paddlex.interpret.lime(img_file,#预测图像路径\n",
    "                      model,#本项目中的最优模型\n",
    "                      num_sampies=3000,#LIME用于学习线性模型的采样数，默认值为3000\n",
    "                      batch_size=50, #预测数据batch大小，默认值为50\n",
    "                      save_dir='./')#可解释性结果可视化保存为.PNG格式的文件和中间文件存储路径"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63bbeb04-0ea8-4807-aa4b-4b2234f04ba6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
